package com.swinginwind.flexutils.components.timeComp.time.timeField
{
	/**
	 * 描述: 
	 * @版权所有: horizo
	 * @创建者: caoww
	 * @创建日期: 2011-1-25 
	 * @修改者: 
	 * @修改日期: 
	 * @修改说明: 
	 */
	
	import com.swinginwind.flexutils.components.timeComp.common.helper.DateHelper;
	import com.swinginwind.flexutils.components.timeComp.time.timeChooser.TimeChooser;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Image;
	import mx.events.FlexMouseEvent;
	
	import spark.components.HGroup;
	import spark.components.PopUpAnchor;
	import spark.components.TextInput;
	import spark.events.TextOperationEvent;

	/**当选择或更改时间时*/
	[Event(name="change", type="flash.events.Event")]
	public class TimeField extends HGroup
	{
		public function TimeField()
		{
			super();
		}
		
		private var _selectedDate:Date;
		[Bindable("change")]
		[Inspectable(category="General")]
		public function get selectedDate():Date
		{
			return _selectedDate;
		}
		/**TimeChooser 控件中选定的日期*/
		public function set selectedDate(value:Date):void
		{
			if(_selectedDate != value){
				_selectedDate = value;
				this.invalidateProperties();
				dispatchEvent(new Event("change"));
			}
		}
		
		private var _selectedDateStr:String;

		public function get selectedDateStr():String
		{
			return _selectedDateStr;
		}
		
		[Bindable("textChanged")]
		public function set selectedDateStr(value:String):void
		{
			if(_selectedDateStr != value){
				_selectedDateStr = value;
				this.invalidateProperties();
				dispatchEvent(new Event("textChanged"));
			}
		}

		
		private var _editable:Boolean=false;
		/**一个标志，指示控件可编辑还是不可编辑，默认是不可编辑*/
		public function get editable():Boolean
		{
			return _editable;
		}
		public function set editable(value:Boolean):void
		{
			if(_editable != value){
				_editable = value;
				this.invalidateProperties();
			}
		}

		/**
		 * 文本字段中所显示日期的格式；默认值：YYYY/MM/DD JJ:NN:SS
		 */
		public var formatString:String="YYYY-MM-DD JJ:NN:SS";
		
		
		
		/**
		 * 设置控件中可选的第一个年份,默认值1900
		 */
		public var minYear:int=1900;
		/**
		 * 控件中可选的最后一个年份,默认值2100
		 */
		public var maxYear:int=2100;
		
		//私有组件
		private var textinput:TextInput;
		private var imgBtn:Image;
		private var timeChooser:TimeChooser;
		private var timeChooserFlag:Boolean;
		private var popupanchor:PopUpAnchor;
		override protected function createChildren():void{
			super.createChildren();
			this.verticalAlign="middle";
			this.gap=0;
			textinput=new TextInput();
			textinput.addEventListener(MouseEvent.CLICK,function():void{
				popTimeChooser();
			});
			this.addElement(textinput);
			imgBtn=new Image();
			imgBtn.source=dataFile;
			imgBtn.addEventListener(MouseEvent.CLICK,function():void{
				popTimeChooser();
			});
			this.addElement(imgBtn);
			timeChooser=new TimeChooser();
			timeChooser.timeField = this;
			timeChooser.maxYear=this.maxYear;
			timeChooser.minYear=this.minYear;
			timeChooser.addEventListener(Event.CHANGE,function():void{
				//timeChange
				textinput.text=DateHelper.dateToString(timeChooser.selectedDate,formatString);
				//textinput.text = "";
				selectedDateStr=textinput.text;
				selectedDate=timeChooser.selectedDate;
			});
			timeChooser.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, function():void {
				popTimeChooser(false);
			});
			/*timeChooser.doubleClickEnabled=true;
			timeChooser.addEventListener(MouseEvent.DOUBLE_CLICK,function():void{
				popTimeChooser(false);
			});*/
			popupanchor=new PopUpAnchor();
			popupanchor.popUp=timeChooser;
			popupanchor.popUpPosition="above";
			this.addElement(popupanchor);
		}
		override protected function commitProperties():void{
			super.commitProperties();
			if(!selectedDate){
				if(this.selectedDateStr){
					selectedDate=new Date(selectedDateStr);
				}
			}
			timeChooser.selectedDate=selectedDate;
			this.textinput.editable=this.editable;
		}
		//弹出timeChooser
		public function popTimeChooser(show:Boolean=true):void{
			if(show)
				popupanchor.displayPopUp=!popupanchor.displayPopUp;
			else
				popupanchor.displayPopUp=false;
		}
		
		//样式
		[Embed(source="assets/CalendarIcon.png")]
		[Bindable]private static var dataFile:Class; 
	}
}